package binarysearchtree;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;//创建根节点

    //最好情况：完全二叉树O(logN)
    //最坏情况：单分支的树O(N)
    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < key) {
                cur = cur.right;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                return true;
            }
        }

        return false;
    }

    //插入
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                //当插入元素已经存在，二叉搜索树中是不能存在的
                return false;
            }
        }

        if (parent.val > key) {
            parent.left = new TreeNode(key);
        } else {
            parent.right = new TreeNode(key);
        }
        return true;
        //相当于尾插
    }

    //删除节点
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        //先找到节点
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                //此时找到了，删除
                removeNode(cur, parent);
            }
        }
    }

    private void removeNode(TreeNode cur, TreeNode parent) {
        //这里面删除cur
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            //此时左右都不为空
            //此时我们找右子树的最小值替换
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            //找最左边的最小值
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }

            cur.val = target.val;
            if (targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }

        }
    }
}
